package com.vcc.boss.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.mybatisflex.core.paginate.Page;
import com.vcc.channel.base.model.request.TransferDto;
import com.vcc.channel.base.service.ChannelBaseService;
import com.vcc.core.annotations.RepeatSubmit;
import com.vcc.core.annotations.RestPack;
import com.vcc.service.boss.request.channel.BossChannelCreateRequest;
import com.vcc.service.boss.request.channel.BossChannelUpdateRequest;
import com.vcc.service.boss.response.channel.BossChannelResponse;
import com.vcc.service.service.ChannelService;
import jakarta.annotation.Resource;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("channel")
public class ChannelController {


    @Resource
    private ApplicationContext applicationContext;
    @Resource
    private ChannelService channelService;


    @SaCheckLogin
    @SaCheckPermission(value = "admin:channel:balance")
    @GetMapping("refreshBalance")
    @RestPack
    public BigDecimal refreshBalance(String channelCode, String type, String accountId) {
        ChannelBaseService channelBaseService = applicationContext.getBean(channelCode, ChannelBaseService.class);
        if (type.equals("main")) {
            return channelBaseService.queryAccountBalance();
        } else if (type.equals("sub")) {
            return channelBaseService.queryAccountBalance(accountId);
        } else {
            throw new IllegalArgumentException("Invalid type: " + type);
        }
    }

    @SaCheckLogin
    @SaCheckPermission(value = "admin:channel:transfer")
    @PostMapping("transfer")
    @RestPack
    public Boolean transfer(@RequestBody TransferDto transferDto) {
        ChannelBaseService channelBaseService = applicationContext.getBean(transferDto.getChannelCode(), ChannelBaseService.class);
        return channelBaseService.transfer(transferDto);
    }


    @RestPack
    @SaCheckLogin
    @SaCheckPermission("card:channel:create")
    @PostMapping("create")
    @RepeatSubmit
    public boolean create(@RequestBody @Validated BossChannelCreateRequest request) {
        return channelService.createChannel(request);
    }


    @SaCheckLogin
    @SaCheckPermission("card:channel:edit")
    @PostMapping("update")
    @RestPack
    @RepeatSubmit
    public boolean update(@RequestBody @Validated BossChannelUpdateRequest request) {
        return channelService.updateChannel(request);
    }

    @GetMapping("paging")
    @RestPack
    @SaCheckLogin
    @SaCheckPermission("card:channel:view")
    public Page<BossChannelResponse> paging(
            Integer pageNumber,
            Integer pageSize,
            String name,
            String code,
            Integer status
    ) {
        return channelService.paging(pageNumber, pageSize, name, code, status);
    }


    @GetMapping("listByChannel")
    @RestPack
    @SaCheckLogin
    @SaCheckPermission("card:channel:view")
    public List<BossChannelResponse> listByChannel(
            String value,
            Boolean hasShareAccount
    ) {
        return channelService.listByChannel(hasShareAccount, value);
    }

}
